package cn.com.surker.weight.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.weight.OrderWeightTypeEnum;
import cn.com.surker.platform.dao.PlatformUserDao;
import cn.com.surker.platform.entity.PlatformUser;
import cn.com.surker.production.entity.vo.ProductionInvoiceVo;
import cn.com.surker.sale.dao.SaleContractDao;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.sale.SaleContractVo;
import cn.com.surker.weight.entity.WeightOrder;
import cn.com.surker.weight.service.ErpWeightOrderService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author luotao
 * @date 2024/11/16
 */
@Service
public class ErpWeightOrderImpl extends WeightOrderServiceImpl implements ErpWeightOrderService {

	@Resource
	private PlatformUserDao platformUserDao;
	@Resource
	private SaleContractDao saleContractDao;

	@Override
	public Integer delTongWeight(String invoiceCode) {
		if (StringUtils.isBlank(invoiceCode)) {

		}
		//	删除之前的旧的过磅数据
		UpdateWrapper<WeightOrder> uw = new UpdateWrapper<>();
		uw.eq("code", invoiceCode);
		uw.eq("order_type", 1);
		uw.eq("scene_type", 1);
		uw.eq("weight_type", OrderWeightTypeEnum.TGB.getCode());
		uw.set("status", StateEnum.DISABLED.getCode());
		int update = weightOrderDao.update(new WeightOrder(), uw);
		return update;
	}

	@Override
	public void beginWeight(Long orgId, String orgName, Long dsId, ProductionInvoiceVo invoice, String remark) {
		if (orgId == null || dsId == null || invoice == null) {
			return;
		}
		//	查询司机
		Long driverId = invoice.getDriverId();
		String openid = null;
		String phone = null;
		if (driverId != null) {
			//	查询司机信息
			PlatformUser platformUser = platformUserDao.selectById(driverId);
			if (platformUser != null) {
				openid = platformUser.getOpenid();
				phone = platformUser.getPhone();
			}
		}
		//	供应商名称,为客户名称和施工方名称
		String conName = invoice.getConName();
		String clientName = invoice.getClientName();
		String supplier = clientName + "[" + conName + "]";
		//	材料名称,为强度等级
		String materialNorms = invoice.getStrengthName();
		//	发货单编码
		String invCode = invoice.getCode();
		//	合同id
		Long conId = invoice.getConId();
		//	查询是否需要打印磅票
		Integer isPrint = 0;
		SaleContractVo saleContractVo = saleContractDao.selectById(conId);
		if (saleContractVo != null) {
			isPrint = saleContractVo.getIsPrint();
		}
		//	删除之前的旧的过磅数据
		UpdateWrapper<WeightOrder> uw = new UpdateWrapper<>();
		uw.eq("code", invCode);
		uw.eq("order_type", 1);
		uw.eq("scene_type", 1);
		uw.eq("weight_type", OrderWeightTypeEnum.TGB.getCode());
		uw.set("status", StateEnum.DISABLED.getCode());
		weightOrderDao.update(new WeightOrder(), uw);
		//	创建过磅数据
		WeightOrder weightOrder = new WeightOrder();
		weightOrder.setCode(invCode);
		weightOrder.setOrgId(orgId);
		weightOrder.setStationId(dsId);
		weightOrder.setStation(orgName);
		weightOrder.setOpenid(openid);
		weightOrder.setPhone(phone);
		weightOrder.setDriver(invoice.getDriver());
		weightOrder.setBusNumber(invoice.getCarNo());
		weightOrder.setIsCheck(1);
		//	毛重
		weightOrder.setOrderStatus(1);
		//	合同运输1
		weightOrder.setOrderType(1);
		//	过磅场景,搅拌站1
		weightOrder.setSceneType(1);
		weightOrder.setWeightType(OrderWeightTypeEnum.TGB.getCode());
		//	供应商名称
		weightOrder.setSupplier(supplier);
		weightOrder.setMaterialNorms(materialNorms);
		weightOrder.setStatus(StateEnum.ENABLED.getCode());
		weightOrder.setCreateTime(invoice.getCreateTime());
		weightOrder.setCreator("erp发货创建");
		weightOrder.setIsPrint(isPrint);
		weightOrder.setRemark(remark);

		weightOrderDao.insert(weightOrder);
	}
}
